package com.lechi.web.system.util;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MybatisMapperUtils {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public Map<Object, Object> getMap1(Map<String, Object> selectParams, String methodName) {
        Map<Object, Object> accVoMap = null;
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.REUSE);
        try (session){
            MapResultHandler handler = new MapResultHandler();
            Configuration configuration = session.getConfiguration();
            configuration.setSafeResultHandlerEnabled(false);
            session.select(methodName, selectParams, handler);
            accVoMap = handler.getMappedResults();
        } catch (Exception ignored) {
        }
        return accVoMap;
    }

    public Map<Object, Object> getMap(Map<String, Object> selectParams, String methodName) {
        //传参
        //创建SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        MapResultHandler handler = new MapResultHandler();
        //具体路径
        sqlSession.select(methodName, selectParams, handler);
        Map<Object, Object> map = handler.getMappedResults();
        sqlSession.close();
        return map;
    }
}
